package com.handtransaction.dao;

import com.handtransaction.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author YAO
 * @create 2021-03-24 15:10
 */
@Repository
public interface UserDao {
//    根据用户id
    @Select("SELECT *" +
            " FROM user WHERE uid=#{uid}")
    public User queryUserByuid(Integer uid);

//    根据用户名查询用户信息
    @Select("SELECT * FROM user " +
            "WHERE uname=#{uname}")
    public User queryUserByuname(String uname);


//    根据学号查询
    @Select("SELECT * FROM user WHERE unumber=#{unumber}")
    public User queryUserByUnumber(Long unumber);


//    保存用户信息
    @Insert("INSERT INTO user(upassword,uname,ufaculty,uphone,upid,ubalance,unumber,usign) " +
            "VALUES (#{upassword},#{uname},#{ufaculty},#{uphone},#{upid},#{ubalance},#{unumber},#{usign})")
    public int SaveUser(User user);

//  根据用户名和密码查询信息
    @Select("SELECT uid,upassword,uname,ufaculty,uphone,upid,ubalance,unumber,usign,uimg_path FROM user " +
            "WHERE uname=#{uname} and upassword=#{upassword}")
    public User queryUserByunameAndupassword(Map<String,String> map);

//    修改用户信息
    @Update("UPDATE user SET uname=#{uname},ufaculty=#{ufaculty},uphone=#{uphone},usign=#{usign},uimg_path=#{uimg_path} WHERE uid=#{uid}")
    public int updateUserInfo(User user);

//    若用户修改了名字，则商品详情表里卖家的名字也应该同步发生修改，同样的，若订单生成，订单详情里也应该有相同的功能，对于卖家和买家的名字问题
    @Update("UPDATE goods SET guname=#{uname} WHERE guid=#{uid}")
    public int updateGOODsUname(User user);

    //    处理充值
    @Update("UPDATE user SET ubalance=#{ubalance}+ubalance WHERE uid=#{uid}")
    public int updateUserBalance(User user);

    /**
     * @param user
     * @return
     */

//    处理用户购买商品后余额的对应减少
          @Update("UPDATE user set ubalance=${ubalance} WHERE uid=#{uid}")
          public int cutUbanlance(User user);

//    处理用户购买购买商品卖家余额的增加
        @Update("UPDATE user set ubalance=${ubalance} WHERE uid=#{uid}")
        public int addUbanlance(User user);

    //    修改用户信息----用于修改密码
    @Update("UPDATE user SET upassword=#{upassword} WHERE uid=#{uid}")
    public int updateUserInfo2(User user);

    //    修改用户权限 -- 封号 / 解封
    @Update("update user set upid=#{upid} where uid=#{uid}")
    int isfrozenUser(User user);

    //用户根据用户名查找用户信息

    @Select("SELECT *" +
            " From user where uname like #{uname}")
    public List<User> queryUserByname(String uname);

    //管理员查询所有用户
    @Select("select * from user")
    List<User> queryAllUser();

    //管理员删除用户
    @Delete("delete from user where uid=#{uid}")
    int deleteUserByAdmin(Integer uid);

}
